package views;

import java.util.StringTokenizer;

import utils.UpdateMap;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.android.google.apis.R;
import com.example.android.google.apis.com.example.android.apis.MapsDemo;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import docks.Dock;
import docks.DockController;

public class BalloonOverlayView<Item extends OverlayItem> extends FrameLayout {

    private LinearLayout layout;
    private TextView title;
    private TextView snippet;
    private ImageView star;
    private Dock dock;
    private View vw;
    private View balloon;
    private int indexx;
    private MapsDemo mapp;
    private Item itemm;
    private BalloonItemizedOverlay bioo;

    /**
     * Create a new BalloonOverlayView.
     * 
     * @param context
     *            - The activity context.
     * @param balloonBottomOffset
     *            - The bottom padding (in pixels) to be applied when rendering
     *            this view.
     */
    public BalloonOverlayView(Context context, int balloonBottomOffset) {

	super(context);

	balloon = this;

	setPadding(10, 0, 10, balloonBottomOffset);
	layout = new LinearLayout(context);
	layout.setVisibility(VISIBLE);

	LayoutInflater inflater = (LayoutInflater) context
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	View v = inflater.inflate(R.layout.balloon_overlay, layout);

	this.vw = v;

	title = (TextView) v.findViewById(R.id.balloon_item_title);
	snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);

	ImageView close = (ImageView) v.findViewById(R.id.close_img_button);
	close.setOnClickListener(new OnClickListener() {
	    public void onClick(View v) {
		layout.setVisibility(GONE);
	    }
	});

	FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
		LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	params.gravity = Gravity.NO_GRAVITY;

	addView(layout, params);

    }

    /**
     * Sets the view data from a given overlay item.
     * 
     * @param item
     *            - The overlay item containing the relevant view data (title
     *            and snippet).
     */
    public void setData(Item item, int index, BalloonItemizedOverlay bio) {

	    StringTokenizer st;
	    String snip;
	    String id;
	    
	    this.itemm = item;
	    this.indexx = index;
	    this.bioo = bio;
	    
	    
	    Log.v("CHIVATO", "SNIPPET: " +  item.getSnippet() + "!!!!");
	    
	    st = new StringTokenizer(item.getSnippet());
	    snip = st.nextToken("@");
	    id = st.nextToken();
	    
	    dock = DockController.getInstance().getDock(Integer.valueOf(id));
	    
		layout.setVisibility(VISIBLE);
		if (item.getTitle() != null) {
			title.setVisibility(VISIBLE);
			title.setText(item.getTitle());
		} else {
			title.setVisibility(GONE);
		}
		if (item.getSnippet() != null) {
			snippet.setVisibility(VISIBLE);
			snippet.setText(snip);
		} else {
			snippet.setVisibility(GONE);
		}

		star = (ImageView) vw.findViewById(R.id.popupstar);
		
		if (dock.isStarred())
		    star.setImageResource(R.drawable.starred);
		else
		    star.setImageResource(R.drawable.no_starred);
		
		star.setOnClickListener(new OnClickListener() {

		    @Override
		    public void onClick(View v) {
			
			DockController.getInstance().changeStarred(dock.getIdDock());
			bioo.onTap(indexx);
			
			Drawable newMarker = null;
			
			// The brand new starred icon is on its way!
			if (dock.isStarred()) { 
			    
			    //green
			    if (dock.getAvailableBikes() != 0 && dock.getFreeSlots() == 0)
				newMarker = getResources().getDrawable(R.drawable.positiongreenstar);
			    //orange
			    if (dock.getAvailableBikes() != 0 && dock.getFreeSlots() != 0) 
				newMarker = getResources().getDrawable(R.drawable.positionorangestar);
			    //red
			    if (dock.getAvailableBikes() == 0 && dock.getFreeSlots() != 0)
				newMarker = getResources().getDrawable(R.drawable.positionredstar);
			// The brand new un-starred icon is on its way!
			} else {
			    
			  //green
			    if (dock.getAvailableBikes() != 0 && dock.getFreeSlots() == 0)
				newMarker = getResources().getDrawable(R.drawable.greenpin);
			    //orange
			    if (dock.getAvailableBikes() != 0 && dock.getFreeSlots() != 0) 
				newMarker = getResources().getDrawable(R.drawable.orangepin);
			    //red
			    if (dock.getAvailableBikes() == 0 && dock.getFreeSlots() != 0)
				newMarker = getResources().getDrawable(R.drawable.redpin);
			}
			

//			newMarker.setBounds(0, 0, newMarker.getIntrinsicWidth(),
//				newMarker.getIntrinsicHeight());
			
			newMarker.setBounds(-newMarker.getIntrinsicWidth() / 2,
		                       -newMarker.getIntrinsicHeight(), 
		                       newMarker.getIntrinsicWidth() / 2,
		                       0);
			 
			    itemm.setMarker(newMarker);
//			}
			
//			UpdateMap.update();
		    }
		});
	}
}
